﻿using LD.Common;
using LD.Host;
using ReaLTaiizor.Controls;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace LD.BC
{
    public partial class FrmAlert : DockContent
    {
        public FrmAlert()
        {
            InitializeComponent();
            GetLang(this.Controls);
            this.TabText = Language.GetString("FrmAlert.TabTextName");
            this.lv_msg.Columns[0].Width = this.lv_msg.Width - 100;
            BCAlertHelper.AlertEvent += LogHelper_EventLog;
        }

        private void GetLang(Control.ControlCollection _controls)
        {
            foreach (Control control in _controls)
            {
                GetLang(control.Controls);
                if (control is MaterialSwitch)
                {
                    control.Text = Language.GetString("FrmAlert." + control.Name);
                }
                else if (control is HopeButton)
                {
                    control.Text = Language.GetString("FrmAlert." + control.Name);
                }
            }
        }

        private void FrmLog_Shown(object sender, EventArgs e)
        {
            Cache.SetCache(CONST.BC_SOUND, true);
            this.sw_io.CheckedChanged -= new System.EventHandler(this.sw_io_CheckedChanged);
            this.sw_io.Checked = (bool)Cache.GetCache(CONST.BC_SOUND);
            this.sw_io.CheckedChanged += new System.EventHandler(this.sw_io_CheckedChanged);
        }

        public void LogHelper_EventLog(string message, LogLevel level)
        {
            //Task.Run(new Action(() =>
            //{
            Color color = Color.Black;
            switch (level)
            {
                case LogLevel.一般:
                    break;
                case LogLevel.警告:
                    color = Color.DarkRed;
                    break;
                case LogLevel.严重:
                    color = Color.Red;
                    break;
                default:
                    break;
            }
            ListViewItem item = new ListViewItem();
            item.ForeColor = color;
            item.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "     " + message;

            this.lv_msg.Invoke(new Action(() =>
                {
                    lv_msg.BeginUpdate();

                    lv_msg.Items.Insert(0, item);

                    lv_msg.EndUpdate();
                }));
            //}));
        }

        private void 复制所选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var listViewItem = this.lv_msg.SelectedItems;
            StringBuilder sb = new StringBuilder();
            foreach (ListViewItem item in listViewItem)
            {
                sb.Append(item.Text);
                sb.Append("\r\n");
            }
            if (sb.Length > 0)
                Clipboard.SetText(sb.ToString());
            sb.Clear();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (this.lv_msg.SelectedItems.Count > 0)
                复制所选ToolStripMenuItem.Visible = true;
            else
                复制所选ToolStripMenuItem.Visible = false;
        }

        private void btn_clearAlert_Click(object sender, EventArgs e)
        {
            BCAlertHelper.Clear();
            this.lv_msg.Items.Clear();
            /*取消报警
            * 先给低电平
            * 再恢复高电平
            */
            var almList = BcFactory.Instance.Config.IoList.Where(p => p.IoType == (int)IoType.输出 &&
                                                                      p.FixType == LightEnum.清除报警.ToString())
                                                          .ToList();
            foreach (var item in almList)
            {
                BcFactory.Instance.Control.SetOut((ushort)item.No, 0);
            }
            Task.Delay(50).Wait();
            foreach (var item in almList)
            {
                BcFactory.Instance.Control.SetOut((ushort)item.No, 1);
            }
            BcFactory.Instance.StartStop.ClosedLight();
            //黄灯
            var yellow = BcFactory.Instance.Config.IoList.Where(p => p.IoType == (int)IoType.输出 && p.FixType == LightEnum.黄灯.ToString()).FirstOrDefault();
            if (yellow != null)
                BcFactory.Instance.Control.SetOut((ushort)yellow.No, 0);

            //蜂鸣
            SoundHelper.Instance.Signal(LevelType.高电平);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void sw_io_CheckedChanged(object sender, EventArgs e)
        {
            if (sw_io.Checked)
            {
                Cache.SetCache(CONST.BC_SOUND, true);
                sw_io.Text = Language.GetString("FrmAlert.sw_io");
            }
            else
            {
                Cache.SetCache(CONST.BC_SOUND, false);
                sw_io.Text = Language.GetString("FrmAlert.Buzzerdisabled");
            }
        }
    }
}
